<?php

class AOrdenInspeccion {

    public function __construct(){
	}
	public function generarOrden(EFacade & $facade)
	{
		$docOI = & $facade->getDocumentRoot();
		$workflow = & $facade->getWorkflow();
		//$OMCh = & $docSLU->ordenMCheckListPrincipal;
		$usuario=NULL;
		//echo '<hr> >> Slu ',$docSLU;
		$ezpdo_= getManager();
		
		
		if($docOI->hayAveria==1){
				$ordenMantenimiento = $facade->session->create('DOrdenMantenimiento');		
				$docOI->ordenGenerada=$ordenMantenimiento;
				$ordenMantenimiento->areaTrabajo=$docOI->areaTrabajo;
				$ordenMantenimiento->tipoMantenimiento=$docOI->tipoMantenimiento;
				$role=NULL;
				$newEfacade =& EFacade :: initWorkflow($facade->session, $ordenMantenimiento, & $role);
				$transSet = & EFacade :: getCurrentTransSetAuto($newEfacade->getWorkflow(),3);//tipo process
				$trans = $transSet->getTransByName('t1');
				$usuario=NULL;
				$placeSet = & EFacade :: getCurrentPlaceSet($newEfacade->getWorkflow());
				$newEfacade->fireTrans($trans, $usuario, "Crear",TRUE);
				//echo "<br><br>la wt es: ";
				$ordenMantenimiento->epSetDirty(TRUE);// marcar el documento para que sea salvado los cambios
				$newEfacade->saveWorkflow();
				$ordenMantenimiento->commit();//echo "<br><br> es:1 ";
				$ordenMantenimiento->numero = $ordenMantenimiento->getDocumentNumber();
				$ordenMantenimiento->commit();
				unset($newEfacade);
				$docOI ->epSetDirty(TRUE);
				$workflowOM = & $docOI->getWorkflowRef();
				//$workflowsSM = & $facade->getWorkflow();
				$workflowOM  ->epSetDirty(TRUE);

				$transSetOSL = & EFacade :: getCurrentTransSetAuto($workflowOM , /* MTrans :: $TYPE_AUTO_AGENT */ 2);
				$listaWT= &$workflowOM->getWorkTransList();

				$transOSL = $transSetOSL->getTransByName('t3');
				$facade->fireTrans($transOSL,$usuario,'Creacion de Orden', TRUE);	
				
		}
		else {
				$docOI ->epSetDirty(TRUE);
				$workflowOM = & $docOI->getWorkflowRef();
				//$workflowsSM = & $facade->getWorkflow();
				$workflowOM  ->epSetDirty(TRUE);

				$transSetOSL = & EFacade :: getCurrentTransSetAuto($workflowOM , /* MTrans :: $TYPE_AUTO_AGENT */ 2);
				$listaWT= &$workflowOM->getWorkTransList();

				$transOSL = $transSetOSL->getTransByName('t4');
				$facade->fireTrans($transOSL,$usuario,'Culminar', TRUE);	
		}
		
		
			
		
	}
	
		public function abortarOrdenes(EFacade & $facade)
	{
		$docOM = & $facade->getDocumentRoot();
		$workflow = & $facade->getWorkflow();
		//$OMCh = & $docSLU->ordenMCheckListPrincipal;
		$usuario=NULL;
		//echo '<hr> >> Slu ',$docSLU;
		$ezpdo_= getManager();
		
		$ordenesTrabajo=$docOM->ordenesTrabajo;
		$solicitudesPartes=$docOM->solicitudPartesAutomatica;
		
		
		
		if(count($ordenesTrabajo)>0 || $ordenesTrabajo!=NULL){
			foreach($ordenesTrabajo as $orden){
				$workflow = & $orden->getWorkflowRef();
				$facade->session->refresh($workflow);
				
				$role=NULL;
				$newEfacade =& EFacade :: initWorkflowWithoutRole($facade->session, $orden, & $role);
				
				$transSet = & EFacade :: getCurrentTransSetAuto($workflow, /*PROCESS */ 3);
				
				$trans = $transSet->getTransByName('t6');
				if($trans==NULL){
					$trans = $transSet->getTransByName('t7');
				}
				else{
					echo "Fatal error ocurrio lo jamas pensado";
				}
				
				$usuario=NULL;
				$newEfacade->fireTrans($trans, $usuario, "Abortar",TRUE);
				//echo "<br><br>la wt es: ";
				$orden->epSetDirty(TRUE);// marcar el documento para que sea salvado los cambios
				$newEfacade->saveWorkflow();
				$orden->commit();//echo "<br><br> es:1 ";
			}
		}
		
		if(count($solicitudesPartes)>0 || $solicitudesPartes!=NULL){
			foreach($solicitudesPartes as $orden1){
				$workflow1 = & $orden1->getWorkflowRef();
				$facade->session->refresh($workflow1);
				
				$role=NULL;
				$newEfacade =& EFacade :: initWorkflowWithoutRole($facade->session, $orden1, & $role);
				
				$transSet = & EFacade :: getCurrentTransSetAuto(/*$workflow*/$newEfacade->getWorkflow(), /*PROCESS */ 3);
				
				$trans = $transSet->getTransByName('t6');
				if($trans==NULL){
					$trans = $transSet->getTransByName('t4');
				}
				else{
					echo "Fatal error ocurrio lo jamas pensado";
				}
				
				$usuario=NULL;
				$newEfacade->fireTrans($trans, $usuario, "Abortar",TRUE);
				//echo "<br><br>la wt es: ";
				$orden1->epSetDirty(TRUE);// marcar el documento para que sea salvado los cambios
				$newEfacade->saveWorkflow();
				$orden1->commit();//echo "<br><br> es:1 ";
				
				unset($newEfacade);
			}
		}
		
		$usuario=NULL;
		////////////////////////////////////////////////////////////////////////
		////////////////////////////////////////////////////////////////////////
		$docOM ->epSetDirty(TRUE);
		
		$workflowsSM = & $facade->getWorkflow();
		$workflowsSM ->epSetDirty(TRUE);

		$transSetOSL = & EFacade :: getCurrentTransSetAuto($workflowsSM , /* MTrans :: $TYPE_AUTO_AGENT */ 2);
		$listaWT= &$workflowsSM->getWorkTransList();

		$transOSL = $transSetOSL->getTransByName('t11');
		$facade->fireTrans($transOSL,$usuario,'Abortar Ordenes', TRUE);	
			
		
	}
	
	
		public function cuentaOrdenes(EFacade & $facade) 
	{	
		$docOM = & $facade->getDocumentRoot();
		
		$workflow = & $facade->getWorkflow();
		//$OMCh = & $docSLU->ordenMCheckListPrincipal;
		$usuario=NULL;
		//echo '<hr> >> Slu ',$docSLU;
		$ezpdo_= getManager();
		
		$ordenesTrabajo=$docOM->ordenesTrabajo;
		$solicitudesPartes=$docOM->solicitudPartesAutomatica;
		$cont=0;
		
		if(count($ordenesTrabajo)>0 || $ordenesTrabajo!=NULL){
			foreach($ordenesTrabajo as $orden){
				$cont++;
			}
		}
		if(count($solicitudesPartes)>0 ||$solicitudesPartes!=NULL){
			foreach($solicitudesPartes as $solicitud){
				$cont++;
			}
		}
		if($cont==1){
			
			$transSetSA = & EFacade :: getCurrentTransSetAuto($workflow, 2); //TYPE AUTO AGENT
			$role=NULL;
			$transSA = $transSetSA->getTransByName('t14');
			$facade->fireTrans($transSA,$role,'Culmina Requisicion', TRUE);
			
		}
		else if($cont>0){
			//echo "aun faltan items";
			
			//echo "<hr>workflowSA: ",$workflowSA;
			$transSetSA = & EFacade :: getCurrentTransSetAuto($workflow, 2); //TYPE AUTO AGENT
			$role=NULL;
			$transSA = $transSetSA->getTransByName('t13');
			$facade->fireTrans($transSA,$role,'Esperar por dem&aacute;s ordenes', TRUE);
			
		}
		
	}
	
		public function evaluarOrdenes(EFacade & $facade) 
		{	
			$docOM = & $facade->getDocumentRoot();
			
			$workflow = & $facade->getWorkflow();
			//$OMCh = & $docSLU->ordenMCheckListPrincipal;
			$usuario=NULL;
			//echo '<hr> >> Slu ',$docSLU;
			$ezpdo_= getManager();
		
			$ordenesTrabajo=$docOM->ordenesTrabajo;
			$solicitudesPartes=$docOM->solicitudPartesAutomatica;
			$numOrdenes=count($ordenesTrabajo);
			$numSolicitudes=count($solicitudesPartes);
			$contSolicitudes=0;
			$contOrdenes=0;
			if($numOrdenes>0 || $ordenesTrabajo!=NULL){
				foreach($ordenesTrabajo as $orden){
					$placeSet = & EFacade :: getCurrentPlaceSet($orden->getWorkflowRef());
					$place=$placeSet->placeStateList[0]->placeRef->name;
					if($place=='p5'){
						$contOrdenes++;
					}
					
				}
			}
			if($numSolicitudes>0 || $solicitudesPartes!=NULL){
				foreach($solicitudesPartes as $solicitud){
					$placeSet = & EFacade :: getCurrentPlaceSet($solicitud->getWorkflowRef());
					$place=$placeSet->placeStateList[0]->placeRef->name;
					if($place=='p5'){
						$contSolicitudes++;
					}
				}
			}
			
			if($numOrdenes==$contOrdenes && $numSolicitudes==$contSolicitudes ){
				$transSetSA = & EFacade :: getCurrentTransSetAuto($workflow, 2); //TYPE AUTO AGENT
				$role=NULL;
				$transSA = $transSetSA->getTransByName('t17');
				$facade->fireTrans($transSA,$role,'Culmina Requisicion', TRUE);
				
			}else{
				$transSetSA = & EFacade :: getCurrentTransSetAuto($workflow, 2); //TYPE AUTO AGENT
				$role=NULL;
				$transSA = $transSetSA->getTransByName('t16');
				$facade->fireTrans($transSA,$role,'En espera de ordenes asociadas', TRUE);
				
				
			}
			
			
			
			///////////////////////////////////////////////////////////////////////////////////////
		
	}
	
	
}
?>